﻿Imports System.Data

Partial Class Content_Reportes_rep_controls_ReporteInfoCliente
    Inherits System.Web.UI.UserControl

    Dim IDataBase As New Clases.DataBase
    'Dim ECConnString As String = ConfigurationManager.AppSettings("dbIP")
    Dim IControles As New Clases.controles

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'IControles.fill_radioListButtonBaseQuery(rblClientes, "tclientes", "cl_id", "nombrec", "select cl_id, NombreC from vwClientes")
            IControles.fill_dropDownListBaseQuery(ddlDespachos, "tdespachos", "desp_id", "desp_nombre", "select * from tdespachos")
            IControles.addNoneAllElement(ddlDespachos)
            IControles.addNoneAllElement(ddlClientes)
            'btn_excel.Visible = True
        End If
    End Sub

    'Protected Sub btnMostrar_Click(sender As Object, e As EventArgs) Handles btnMostrar.Click
    '    MsgBox(rblClientes.SelectedValue & " " & rblClientes.SelectedItem.ToString, MsgBoxStyle.ApplicationModal, "Alerta")
    '    Dim tablaCliente As New DataTable
    '    Dim query As String = "select * from vwInfoClientes where ID = " & rblClientes.SelectedValue

    '    IDataBase.createDataTable(tablaCliente, query)

    '    If tablaCliente IsNot Nothing Then
    '        lblNombre.Text = tablaCliente.Rows(0)("nombre")
    '        lblApellidoP.Text = tablaCliente.Rows(0)("Apellido Paterno")
    '        lblApellidoM.Text = tablaCliente.Rows(0)("Apellido Materno")
    '        lblRFC.Text = tablaCliente.Rows(0)("RFC")
    '        lblCurp.Text = tablaCliente.Rows(0)("CURP")
    '        lblDireccion.Text = tablaCliente.Rows(0)("Dirección")
    '        lblCP.Text = tablaCliente.Rows(0)("Código Postal")
    '        lblEstado.Text = tablaCliente.Rows(0)("Estado")
    '        lblCiudad.Text = tablaCliente.Rows(0)("Ciudad")
    '        lblCorreo.Text = tablaCliente.Rows(0)("Correo Electrónico")
    '    End If

    'End Sub

    'Protected Sub rblClientes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles rblClientes.SelectedIndexChanged
    '    Dim tablaCliente As New DataTable

    '    Session("cliente_id") = rblClientes.SelectedValue

    '    Dim query As String = "select * from vwInfoClientes where ID = " & Session("cliente_id")

    '    IDataBase.createDataTable(tablaCliente, query)

    '    If tablaCliente IsNot Nothing Then
    '        lblNombre.Text = tablaCliente.Rows(0)("nombre")
    '        lblApellidoP.Text = tablaCliente.Rows(0)("Apellido Paterno")
    '        lblApellidoM.Text = tablaCliente.Rows(0)("Apellido Materno")
    '        lblRFC.Text = tablaCliente.Rows(0)("RFC")
    '        lblCurp.Text = tablaCliente.Rows(0)("CURP")
    '        lblDireccion.Text = tablaCliente.Rows(0)("Dirección")
    '        lblCP.Text = tablaCliente.Rows(0)("Código Postal")
    '        lblEstado.Text = tablaCliente.Rows(0)("Estado")
    '        lblCiudad.Text = tablaCliente.Rows(0)("Ciudad")
    '        lblCorreo.Text = tablaCliente.Rows(0)("Correo Electrónico")
    '    End If
    'End Sub

    Protected Sub exportToExcel(ByVal query As String)
        Dim gridView_toExport As New GridView
        IControles.fillGridViewPaging(gridView_toExport, Session("InfoClientes"))

        For i As Integer = 0 To gridView_toExport.Rows.Count - 1
            Dim row As GridViewRow = gridView_toExport.Rows(i)
        Next

        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As IO.StringWriter = New IO.StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        gridView_toExport.AllowPaging = False
        form.Controls.Add(gridView_toExport)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"

        Response.AddHeader("Content-Disposition", "attachment;filename=InfoCliente" & Now.ToString("_dd_MMMM_yy_HH_mm_ss") & ".xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()

    End Sub

    'Protected Sub btnGenerar_Click(sender As Object, e As EventArgs) Handles btnGenerar.Click
    '    'Session("InfoClientes") = "select Nombre, [Apellido Materno], [Apellido Paterno], RFC, CURP, Calle, Numero, Colonia, Pais, Estado, Municipio, [Código Postal], [Correo Electrónico] from vwInfoClientes where ID = " & Session("cliente_id")
    '    'Session("InfoClientes") = "select * from vwInfoClientes where ID = " & Session("cliente_id")
    '    'Response.Redirect("~/Content/Reportes/infoCliente.aspx")
    '    exportToExcel(Session("InfoClientes"))
    'End Sub

    Protected Sub ddlDespachos_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlDespachos.SelectedIndexChanged
        If ddlDespachos.SelectedValue <> "None" Then
            IControles.fill_dropDownListBaseQuery(ddlClientes, "tclientes", "cl_id", "nombre", "select cl_id, cl_nombre + ' ' + cl_apellido_p + ' ' + cl_apellido_m as nombre from TClientes where desp_id = " & ddlDespachos.SelectedValue)
            IControles.addNoneAllElement(ddlClientes)
        Else
            ddlClientes.Items.Clear()
            IControles.addNoneAllElement(ddlClientes)
        End If
    End Sub

    Protected Sub ddlClientes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlClientes.SelectedIndexChanged
        Dim tablaCliente As New DataTable

        Session("cliente_id") = ddlClientes.SelectedValue

        'Dim query As String = "select * from vwInfoClientes where ID = " & Session("cliente_id")
        Dim query As String = "select Nombre, [Apellido Materno], [Apellido Paterno], RFC, CURP, Calle, Numero, Colonia, Pais, Estado, Municipio, [Código Postal], [Correo Electrónico] from vwInfoClientes where ID = " & Session("cliente_id")
        Session("InfoClientes") = query
        IDataBase.createDataTable(tablaCliente, query)

        If tablaCliente IsNot Nothing Then
            lblNombre.Text = tablaCliente.Rows(0)("Nombre")
            lblApellidoP.Text = tablaCliente.Rows(0)("Apellido Paterno")
            lblApellidoM.Text = tablaCliente.Rows(0)("Apellido Materno")
            lblRFC.Text = tablaCliente.Rows(0)("RFC")
            lblCurp.Text = tablaCliente.Rows(0)("CURP")
            lblCalle.Text = tablaCliente.Rows(0)("Calle")
            lblNumero.Text = tablaCliente.Rows(0)("Numero")
            lblColonia.Text = tablaCliente.Rows(0)("Colonia")
            lblPais.Text = tablaCliente.Rows(0)("Pais")
            lblEstado.Text = tablaCliente.Rows(0)("Estado")
            lblCiudad.Text = tablaCliente.Rows(0)("Municipio")
            lblCP.Text = tablaCliente.Rows(0)("Código Postal")
            lblCorreo.Text = tablaCliente.Rows(0)("Correo Electrónico")
        End If
        btn_excel.Visible = True
    End Sub

    Protected Sub btn_excel_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_excel.Click
        exportToExcel(Session("InfoClientes"))
    End Sub
End Class
